package com.zjt;

import com.zjt.mapper.ProductMapper;
import com.zjt.mapper.UserMapper;
import com.zjt.po.Product;
import com.zjt.po.ProductTypeVo;
import com.zjt.po.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

public class test {
    private SqlSession sqlSession;
    private ProductMapper productMapper;
    private UserMapper userMapper;
    @Before
    public void before() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
        productMapper = sqlSession.getMapper(ProductMapper.class);
    }
    @After
    public void after(){
        sqlSession.close();
    }

    @Test
    public void test1() {
        ProductTypeVo productProductTypeVo = productMapper.queryProductTypeVoByProductId(1);
        System.out.println(productProductTypeVo);
    }
    @Test
    //一对一
    public void test2(){
        Product product = productMapper.queryProductProductTypeVoByProductId2(1);
        System.out.println(product);
        System.out.println(product.getProductType());
    }
    @Test
    //一对多
    public void test3(){
        userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryUserRelationRolesByUserId(1);
        System.out.println(user);
        System.out.println(user.getRoles());
    }
    @Test
    //多对多1
    public void test4(){
        userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryUserAllRelationRoles();
        for(User user : users){
            System.out.println(user);
            System.out.println(user.getRoles());
        }
    }
    @Test
    //多对多2
    public void test5() {
        userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryUserAllRelationRoles2();
        users.forEach(user -> {
            System.out.println(user);
            System.out.println(user.getRoles());
        });
    }
    @Test
    //延迟加载
    public void test6(){
        userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryUserAllRelationRoles2();
        users.forEach(user->{
            System.out.println(user.getUserName());
        });
    }
}
